Ext.require([ 
    'Ext.util.*', 
    'Ext.data.*'  
]);  
Ext.onReady(function(){
	var options = {
    chart:{   
		//将报表对象渲染到层上
		defaultSeriesType:'spline'
	},
    title: {
        text: ''
    },
    xAxis: {},
    yAxis: {
    title: {
        text: '数值'
    },
    plotLines: [{
        value: 0,
        width: 1,
        color: '#808080'
    }]
	},
    series: []
};

//查看明细
function exportViewProdToExcel(param){
	$.fileDownload(path+"/sssj/sssj/hs_downloadData.action", {
		httpMethod: "POST",
		data: $.param(param, true)
	});
	// var proids = new Array();
	// var proidEl = document.getElementsByName("proid");
	// for(var i = 0;i < proidEl.length;i ++){
	// 	if(proidEl[i].checked)
	// 		proids.push(proidEl[i].value);
	// }
	// if(proids.length==0){
	// 	if(confirm("将要导出当前查询到的所有产品信息,是否继续？")){
	// 		$.fileDownload("${request.contextPath}/product/exportWaitViewProd?"+$('#queryForm').serialize(), {
	// 			httpMethod: "POST"
	// 		});
	// 	}else{
	// 		return;
	// 	}
	// }else{
	// 	$.fileDownload("${request.contextPath}/product/exportWaitViewProd", {
	// 		httpMethod: "POST",
	// 		data: $.param({idStr : proids.join(",")}, true)
	// 	});
	// }
}

function loadDay(url,el){
	Ext.Ajax.request({
	     url: path+url,
	     async:false,
	     success:function(response) {
	     	options.chart['renderTo']=el;
	     	var json= eval("("+response.responseText+")");
	     	options.series =json.storeY;
	     	if(json.storeX!=null){
	     		options.xAxis =json.storeX;
				var periodType = 12;
				//periodType 1=日 2=周 3=月
				//周报表取间隔24个点，其它的取12个点
				if(json.periodType==3){
					periodType = 24
				}
				options.xAxis.tickInterval = periodType;
	     	}
	     	new Highcharts.Chart(options);
	     	Ext.getCmp("tabpanel").doLayout();
	     },
	     failure:function() {
	         Ext.Msg.alert("提示","加载出现异常了！");
	     }
	});
}
//显示 
Ext.create('Ext.Viewport', {
	layout:'fit',
	items:{
		xtype:"tabpanel",
		id:'tabpanel',
		resizable:false, 
		closable:false,
		activeTab: 0,     // 初始打开第一个标签页
		modal:true,
		items:[
				{
					xtype:'form',
					title:'日统计',
					id:'day',
					border:false,
					items:[
						{
							xtype:'panel',
							layout:'column',
							border:false,
							items:[
							{
								xtype: 'textfield',
								id:'beginDate',
								labelWidth:40,
								padding:'5 10 5 10',
								fieldLabel : '时间',
								editable:false,
								listeners:{
									render:function(p){
										p.getEl().on('click',function(){
											//el属性的作用：将日期控件返回给id="el属性值"的元素
											//这里应该配置成与id一致
											WdatePicker({el:'beginDate-inputEl',dateFmt:'yyyy-MM-dd',maxDate:'%y-%M-%d'});
										});
									}
								}
							},
							{
								xtype:"combobox",
								fieldLabel:"地点",
								id: "type",
								editable:false,
								triggerAction:'all',
								store:new Ext.data.JsonStore({
									fields:[{name:"id"},{name:"name"}],
									data: [{"id":"0","name":"蔡家嘴"},{"id":"2","name":"宗关"},{"id":"1","name":"紫阳湖"},{"id":"3","name":"沈阳路"}]
								}),
								labelAlign : 'right',
								valueField: 'id',
								displayField: 'name',
								queryMode:'local',
								value:"蔡家嘴",
								selectOnFocus:true,
								listeners:{
									"select":function(combo,record,opts){
										Ext.getCmp("type").setValue(record[0].get("id"));
									}
								}
							}
							,{
								 xtype:'button',
								 text:'查询',
								 width:'50px',
								 margin:'5 10 5 10',
								 handler:function(){
								 loadDay('/sssj/sssj/hs_getStatisticsDay.action?time='+Ext.getCmp("beginDate").getValue()+"&type="+Ext.getCmp("type").getValue(),'dayD');
								}
							 }
							]
						},{
							xtype:'panel',
							contentEl:'dayD'
						}
					]
				}, {
					xtype:'form',
					title:'周统计',
					id:'week',
					border:false,
					items:[
						{
							xtype:'panel',
							layout:'column',
							border:false,
							items:[
								{
									xtype:"combobox",
									fieldLabel:"地点",
									id: "typeWeek",
									editable:false,
									triggerAction:'all',
									store:new Ext.data.JsonStore({
										fields:[{name:"id"},{name:"name"}],
										data: [{"id":"0","name":"蔡家嘴"},{"id":"2","name":"宗关"},{"id":"1","name":"紫阳湖"},{"id":"3","name":"沈阳路"}]
									}),
									labelAlign : 'right',
									valueField: 'id',
									displayField: 'name',
									queryMode:'local',
									selectOnFocus:true,
									value:"蔡家嘴",
									listeners:{
										"select":function(combo,record,opts){
											Ext.getCmp("typeWeek").setValue(record[0].get("id"));
										}
									}
								}
								,{
									xtype:'button',
									text:'查询',
									width:'50px',
									margin:'5 10 5 10',
									handler:function(){
										loadDay('/sssj/sssj/hs_getStatisticsWeek.action?typeWeek='+Ext.getCmp("typeWeek").getValue(),'weekD');
									}
								}
							]
						},{
							xtype:'panel',
							contentEl:'weekD'
						}]
				}, {
					xtype:'form',
					id:'month',
					title:'月统计',
					border:false,
					items:[
						{
							xtype:'panel',
							layout:'column',
							border:false,
							items:[
							{
								xtype: 'textfield',
								id:'beginMonth',
								labelWidth:40,
								padding:'5 10 5 10',
								fieldLabel : '时间',
								editable:false,
								listeners:{
									render:function(p){
									p.getEl().on('click',function(){  
									WdatePicker({el:'beginMonth-inputEl',dateFmt:'yyyy-MM',maxDate:'%y-%M'});
									});
									}
								}
							},
							{
								xtype:"combobox",
								fieldLabel:"地点",
								value:"蔡家嘴",
								id: "typeMonth",
								editable:false,
								triggerAction:'all',
								store:new Ext.data.JsonStore({
									fields:[{name:"id"},{name:"name"}],
									data: [{"id":"0","name":"蔡家嘴"},{"id":"2","name":"宗关"},{"id":"1","name":"紫阳湖"},{"id":"3","name":"沈阳路"}]
								}),
								labelAlign : 'right',
								valueField: 'id',
								displayField: 'name',
								queryMode:'local',
								selectOnFocus:true,
								listeners:{
									"select":function(combo,record,opts){
										Ext.getCmp("typeMonth").setValue(record[0].get("id"));
									}
								}
							},
							{
								 xtype:'button',
								 text:'查询',
								 width:'50px',
								 margin:'5 10 5 10',
								 handler:function(){
									 var type = Ext.getCmp("typeMonth").getValue();
								 loadDay('/sssj/sssj/hs_getStatisticsMonth.action?time='+Ext.getCmp("beginMonth").getValue()+"&typeMonth="+type,'monthD');
								}
							}
							]
						},{
							xtype:'panel',
							contentEl:'monthD'
						}
					]
				}, {
					xtype:'form',
					id:'dataDownloadD',
					title:'数据下载',
					border:false,
					items:[
						{
							xtype:'panel',
							layout:'column',
							border:false,
							items:[
								{
									xtype: 'textfield',
									id:'downloadBeginDay',
									//labelWidth:40,
									padding:'10 0',
									fieldLabel : '开始日期',
									labelAlign : 'right',
									editable:false,
									listeners:{
										render:function(p){
											p.getEl().on('click',function(){
												WdatePicker({el:'downloadBeginDay-inputEl',dateFmt:'yyyy-MM-dd',maxDate:'%y-%M-%d'});
											});
										}
									}
								},{
									xtype: 'textfield',
									id:'downloadEndDay',
									padding:'10 0',
									fieldLabel : '结束日期',
									labelAlign : 'right',
									editable:false,
									listeners:{
										render:function(p){
											p.getEl().on('click',function(){
												WdatePicker({el:'downloadEndDay-inputEl',dateFmt:'yyyy-MM-dd',maxDate:'%y-%M-%d'});
											});
										}
									}
								}, {
									xtype:"combobox",
									fieldLabel:"站点名称",
									id: "downloadBdzCode",
									padding:'10 0',
									labelAlign : 'right',
									editable:false,
									triggerAction:'all',
									store:new Ext.data.JsonStore({
										fields:[{name:"id"},{name:"name"}],
										data: [
											{"id":"13MT0227","name":"蔡家嘴变电站"},
											{"id":"02715071016_ESM100","name":"宗关变电站"},
											{"id":"02715071015_ESM100","name":"紫阳湖变电站"},
											{"id":"02715071017_ESM100","name":"沈阳路变电站"}
										]
									}),
									valueField: 'id',
									displayField: 'name',
									value:"13MT0227",
									queryMode:'local'
								}, {
									xtype:"combobox",
									fieldLabel:"数据类型",
									id: "dataType",
									padding:'10 0',
									labelAlign : 'right',
									editable:false,
									triggerAction:'all',
									store:new Ext.data.JsonStore({
										fields:[{name:"id"},{name:"name"}],
										data: [
											{"id": 1,"name":"分钟均值数据"},
											{"id": 2,"name":"小时均值数据"},
											{"id": 3,"name":"日均值数据"}
										]
									}),
									valueField: 'id',
									displayField: 'name',
									value:1,
									queryMode:'local',
									selectOnFocus:true
								},
								{
									xtype:'button',
									text:'下载',
									width:'50px',
									margin:'10',
									handler:function(){
										var dataType = Ext.getCmp("dataType").getValue();
										var downloadBdzCode = Ext.getCmp("downloadBdzCode").getValue();
										//日期不填时，得到的值是空串
										var downloadBeginDay = Ext.getCmp("downloadBeginDay").getValue();
										if(downloadBeginDay==''){
											Ext.Msg.alert("提示","请选择开始日期");
											return;
										}
										var downloadEndDay = Ext.getCmp("downloadEndDay").getValue();
										if(downloadEndDay==''){
											Ext.Msg.alert("提示","请选择结束日期");
											return;
										}
										//loadDay('/sssj/sssj/hs_getStatisticsMonth.action?time='+Ext.getCmp("beginMonth").getValue()+"&typeMonth="+type,'monthD');
										var param = {
											dataType:dataType,
											downloadBdzCode:downloadBdzCode,
											downloadBeginDay:downloadBeginDay,
											downloadEndDay:downloadEndDay
										};
										exportViewProdToExcel(param);
									}
								}
							]
						}
						,{
							xtype:'panel',
							html:'提示：<br>' +
							'1、可下载到当前日期前一天示范站点的日均值数据<br>'+
							'2、可下载到当天已统计的分钟均值数据<br>'+
							'3、数据下载方法是，根据您的需要分别组合站点名称、数据类型、下载数据开始日期及下载数据截至日期等条件'
						}

					]
				}
		],
		listeners:{
			'tabchange':function(tabPanel, newCard){
				if(newCard.getId()=="day"){
					loadDay('/sssj/sssj/hs_getStatisticsDay.action','dayD');
				}
				else if(newCard.getId()=="week"){
					loadDay('/sssj/sssj/hs_getStatisticsWeek.action','weekD');
				}
				else if(newCard.getId()=="month"){
					loadDay('/sssj/sssj/hs_getStatisticsMonth.action','monthD');
				}else{

				}
			}
		}
	}
});
loadDay("/sssj/sssj/hs_getStatisticsDay.action","dayD");	     
});
